package leetcode.weekly.week294;

import java.util.Arrays;

//Solution3Test
public class Solution3 {

	public int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

	public int minimumLines(int[][] stockPrices) {
		Arrays.sort(stockPrices, (a, b) -> a[0] - b[0]);
		int d = stockPrices[0][0], p = stockPrices[0][1];
		int ldd = -1, lpd = -1;
		int ans = 0;
		for (int i = 1; i < stockPrices.length; i++) {
			int[] cur = stockPrices[i];
			int day = cur[0], price = cur[1];
			int dd = day - d, pd = price - p;
			int gcd = this.gcd(day - d, price - p);
			int di = dd / gcd, pi = pd / gcd;
			if (di != ldd || pi != lpd) {
				ans += 1;
				ldd = di;
				lpd = pi;
			}
			d = day;
			p = price;
		}
		return ans;
	}

}
